package com.offcn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.offcn.bean.User;
import com.offcn.bean.UserExample;
import com.offcn.mapper.UserMapper;
import com.offcn.utils.ReturnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public PageInfo<User> userPageInfo(Integer pageNo,String uname) {
        //注入分页
        PageHelper.startPage(pageNo,3);

        UserExample userExample = new UserExample();
        userExample.createCriteria().andUnameLike("%"+uname+"%");
        List<User> userList = userMapper.selectByExample(userExample);
        PageInfo<User> userPageInfo = new PageInfo<User>(userList,4);
        return userPageInfo;
    }

    @Override
    public ReturnResult modefyStatus(Integer status, Integer uid) {
        User user = new User();
        user.setStatus(status);
        user.setUid(uid);
        try {
            int update = userMapper.updateByPrimaryKeySelective(user);
            return new ReturnResult("状态修改成功!",1,null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ReturnResult("状态修改失败!",-1,null);
    }

    @Override
    public User queryDetails(Integer uid) {
        return userMapper.selectByPrimaryKey(uid);
    }

    @Override
    public User getUserByUnamePwd(String uname, String password) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUnameEqualTo(uname).andUpassEqualTo(password);
        List<User> userList = userMapper.selectByExample(userExample);
        //没查到的话有可能不为空但是长度绝对为0
        if(userList!=null && userList.size()>0){
            User user = userList.get(0);
            return user;
        }
        return null;
    }
}
